<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/head.jsp" %>

<!-- 时间日期插件 -->
<link href="<%=request.getContextPath()%>/static/plugin/My97DatePicker/skin/default/datepicker.css" rel="stylesheet" >
<script src="<%=request.getContextPath()%>/static/plugin/My97DatePicker/WdatePicker.js"></script>
<style>
.ordercount{
	width:100%;
    display: inline-flex;
}
.samestatic{
	width:309px;
	height:150px;
	margin-top:10px;
	margin-left:77px;
}
.samestatic:nth-child(1){
	margin-left:0px;
}
.ordersty{
	background:#1f95ef;
}
.balance{
	background:#45b854;
}
.customer{
	background:#f86b67;
}
.sametitle{
	color: #fff;
    font-size: 16px;
    margin-left: 20px;
    margin-top: 10px;
}
.sameorderinfo{
	color: #fff;
    margin-left: 35px;
    line-height:20px;
}
.summer-datagrid-cell{
	text-align:center;
}
.top-batch{
    width:80px;
	height:34px;
	text-align:center;
}
/*******批量备注*******/
.remarkcon{
    height: 77px;
    background: #fff;
    border: solid 1px #e1e6eb;
    border-bottom: 0;
}
.remarkbut{
	width:114px;
	height:34px;
	border:solid 1px #f86b67;
	line-height:34px;
	text-align:center;
	color:#f86b67;
	margin:20px;
	cursor:pointer;
}
.smallremark{
	color: #1f95ef;
    font-size: 10px;
    margin-left: 7px;
}
.copybut{
	color:#0474c8;
	margin-left: 5px;
	cursor:pointer;
    margin-right: 8px;
}
/***按钮选中态****/
.pitchbut{
	background:#f86b67;
	color:#fff;
}
/*******表格样式*********/
.proleft{
  float:left;
  margin-top: 2px;
  width: 33%;
}
.proleft>img{
  width:84px;
  height:84px;
}
.proright{
  float:left;
  margin-left:2px;
  margin-top: 2px;
  text-align: left;
      width: 66%;
  
}
.sametr{
	height: 110px;
    border-bottom: solid 1px #999;
}
.summer-top{
	padding:0px !important;
}
/*******head******/
.headcon{
    width: 100%;
    border-bottom: solid 1px #0288d1;
}
.top-batch{
	width:100%;
	line-height:34px;
}
</style>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    
</div>

<form id="form" method="post">
    <div class="form-wrapper">
      <div class="form-cont">
      <p class="form-cont-title">排序</p>
      <input type="text" class="summer-input" id="reorder"/>
      <input type="hidden" class="summer-input" id="msg"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">顶部图片</p>
      <input type="hidden" class="summer-input" id="topImg"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">标识图</p>
      <input type="hidden" class="summer-input" id="img"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">标题</p>
      <input type="text" class="summer-input" id="title"/>
      </div>
      <div class="form-cont" id="goodsList">
      </div>
     <div class="form-btn-wrapper" id="funcbar">
     	<a class="btn btn-normal" id="delBtn">删除商品</a>
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
</form>

<div class="search-field">  
	<span class="field-cont">
        <span class="field-cont-title">商品名称</span>
        <input type="text" class="summer-input" id="goodsName"/>
        <input type="hidden" class="summer-input" id="shareId"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">商品类型</span>
        <input type="text" class="summer-input" id="spuPlatform"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">商品分类</span>
        <input type="text" id="categoryId" name="categoryId" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[500]']"/> 
        
    </span><br/>
   
     <span class="field-cont">
        <span class="field-cont-title">是否爆款</span>
        <input type="text" class="summer-input" id="isHot"/>
    </span><br/>
     <span class="field-cont">
        <span class="field-cont-title">佣金排序</span>
        <input type="text" class="summer-input" id="wlCommission"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">佣金比例排序</span>
        <input type="text" class="summer-input" id="wlCommissionShare"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">时间</span>
        <input type="text" class="summer-input" id="createTime"/>
    </span>

    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>


	<div class="remarkcon">
		<div class="remarkbut" id="batchAddHot"><a>设置爆品</a></div>
	</div>
	
	<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="10%">
	    <col width="10%">
	    <col width="30%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="25%">	
	    <col width="20%">		    	    
	    <thead>
		     <tr>
		    <th><div class="summer-datagrid-cell"></div></th>
		    <th><div class="summer-datagrid-cell">排序</div></th>
		    <th><div class="summer-datagrid-cell">名称</div></th>
		    <th><div class="summer-datagrid-cell">图片</div></th>
		    <th><div class="summer-datagrid-cell">类型</div></th>
		    <th><div class="summer-datagrid-cell">分类</div></th>
		    <th><div class="summer-datagrid-cell">价钱（原价、券后价、佣金数）</div></th>
		    <th><div class="summer-datagrid-cell">时间</div></th>
		    <th><div class="summer-datagrid-cell">操作</div></th>
			 </tr>
	    </thead>
    </table>
</div>

<script>
	//请求url
	var CONST_URL = {
		INIT_MAIN: basePath + '${common_url}initMarket',
		INIT_HOT: basePath + '${common_url}initHot',
		INIT_SHARE: basePath + '${common_url}initShare',
		DEL: basePath + '${common_url}deljx',
		MARKET_LIST: basePath + '${common_url}marketList',
		INIT_REORDER: basePath + '${common_url}initReorder',
		INIT_GOODS_LIST: basePath + '${common_url}initGoods',
		GET_CATEGORY_ID: basePath + '${common_url}getGoodsCategory',
		SET_HOT: basePath + '${common_url}setHot',
		SAVE_HOT: basePath + '${common_url}saveHot',
		DEL_HOT_GOODS: basePath + '${common_url}delHotGoods',
		INIT_EDIT_HOT: basePath + '${common_url}initEditHot',
		GOODS_LIST: basePath + '${common_url}goodsList'
	};
	
    var  wlcReorder,
   		 wlcsReorder,
    	 timeReorder,
    	 editData,
    	 goodsList,
    	 shareSpuIds,
    	 permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {   	
    	editData = ${editData};
    	goodsList = editData.goodsList;
    	$("#reorder").val(editData.reorder);
    	$("#title").val(editData.title);
    	$("#img").val(editData.img);
    	$("#topImg").val(editData.topImg);
    	$("#msg").val(${msg});
    	var msg = $("#msg").val();
    	if(msg != "null" ){
    		alert(msg);
    	}
    	$("#shareId").val(editData.shareId);
    	//编辑信息列表展示
   	 	$('#form').form({
			onLoadSuccess : function(data) {
			}
		}).form('loadDataEdit', {
			data : editData
		});
    	 //初始化上传
        $("#img").filePlugin({
       	 type:'image'
        });
        $("#topImg").filePlugin({
       	 type:'image'
        });
		//$('#saveBtn').on('click', submit);
		//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:saveHot']
        }));
		
     	 //保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:saveHot"]', {isContinue: false},  submit);
		
      	//设置爆品
		$('#t-wrap').on('click','a[data-permit="${name_english}:setHot"]',
				function() {
					var $this = $(this), 
					id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
					shareId = editData.shareId;
					window.location.href = CONST_URL.SET_HOT + "?shareId=" + shareId+"&spuId="+id;
				});
      
    	var html = null;
    	for(var i=0;i<goodsList.length;i++){
    		var j = i + 1;
    		if(html == null){
    			html = '<div style="float:left;width:20%;">'
    				   +'<input type="hidden" id="spuId'+i+'" value="'+goodsList[i].spuId+'">'
    				   +'<div style="width:148px;height:148px"><img style="width:100%;height:100%;" src="'+goodsList[i].img+'"></div>'
    				   +'<div style="width:90%;height:100px;"><p>'+goodsList[i].goodsName+'</p></div>'
    				   +'<div><input type="text" id="shareReorder" value="'+j+'"></div>'
    				   +'<div><input type="checkbox" id="shareSpuId" name="shareSpuId" value="'+goodsList[i].spuId+'"></div>'
    				   +'</div>';
    		}else{
    			html = html +  '<div style="float:left;width:20%;">'
				   +'<input type="hidden" id="spuId'+i+'" value="'+goodsList[i].spuId+'">'
				   +'<div style="width:148px;height:148px"><img style="width:100%;height:100%;" src="'+goodsList[i].img+'"></div>'
				   +'<div style="width:90%;height:100px;"><p>'+goodsList[i].goodsName+'</p></div>'
				   +'<div><input type="text" id="shareReorder" value="'+j+'"></div>'
				   +'<div><input type="checkbox" id="shareSpuId" name="shareSpuId" value="'+goodsList[i].spuId+'"></div>'
				   +'</div>';
    		}
    	}
    	$("#goodsList").append(html);
    	
    	
    	//下拉列表初始化
    	 $('#spuPlatform').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_SPU_PLATFORM'
         });
    	 $('#isHot').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_IS_HOT'
         });
    	 //T_DM_T_GOODS_SPU_WL_COMMISSION
    	 $('#wlCommission').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_WL_COMMISSION'
         });
    	 //T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE
    	 $('#wlCommissionShare').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE'
         });
    	 //T_DM_T_GOODS_SPU_CREATE_TIME
    	 $('#createTime').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_CREATE_TIME'
         });
    	 
    	//初始化下拉选项  商品所属公司
    	 $('#categoryId').combobox({
             url : CONST_URL.GET_CATEGORY_ID, 
             valueField : 'id',
             async:false,
             textField : 'name'
         });
    	
    	 //排序窗口跳转
    	 var iWidth;                         //弹出窗口的宽度;
         var iHeight;                        //弹出窗口的高度;
         var iTop = (window.screen.height-60-iHeight)/2;       //获得窗口的垂直位置;
         var iLeft = (window.screen.width-20-iWidth)/2;        //获得窗口的水平位置;
 		
 		//删除商品
 		$('#delBtn').on('click',function(){
			//选中勾选框商品删除
			var shareSpuId = $("input[name='shareSpuId']:checked").serialize();
			shareId = editData.shareId;
			$.sdialog({
					type : 'confirm',
					msg : '您确定要删除这些商品吗?',
					onConfirm : function(result) {
						if (result) {
							$.sajax({
								url : CONST_URL.DEL_HOT_GOODS + "?shareId=" + shareId+"&shareSpuId="+shareSpuId,
								onLoadSuccess : function() {
									$.stip({
										type : 'ok',//显示类型 有info、 ok、 danger
										text : '删除成功！'
									});
									refresh();
								}
							});

						}
					}
				});
		});
 		//返回按钮
    	$('#backBtn,#goBack').on('click',backMain);
    	//跳转爆款页
    	$('#tohot').on('click',toHot);
    	//跳转分享朋友圈页
    	$('#tosharepyq').on('click',toShare);
    	$('#togoods').on('click',toGoodsList);
    	//批量设置爆品
    	$('#batchAddHot').on('click',tobatchAddHot);
    	/************/
       
        $('#search').on('click', reloadTable);
        $('#reset').on('click', function() {
			//重置文本框的值
			$('#goodsName').val('');
			$('#spuPlatform').combobox('clear');
			$('#categoryId').combobox('clear');
			$('#isJingping').val('');
			$('#isHot').combobox('clear');
			$('#wlCommission').combobox('clear');
			$('#wlCommissionShare').combobox('clear');
			$('#createTime').combobox('clear');
			reloadTable();
		});
        
		createTable();
	});
    //批量设置爆品
    function tobatchAddHot(){
    	var shareId = editData.shareId;
    	var id = $("input[name='id']:checked").serialize();
		window.location.href = CONST_URL.SET_HOT + "?shareId="+shareId+"&id="+id;
    }
    function refresh(){
    	shareId = editData.shareId;
    	window.location.href = CONST_URL.INIT_EDIT_HOT+"?shareId="+shareId;
    }
  //表单提交
	function submit() {
		var reorder = $("#reorder").val();
    	var title = $("#title").val();
    	var img = $("#img").val();
    	var topImg = $("#topImg").val();
    	var spuIds = null;
    	var mainImages = null;
    	//遍历商品，获取商品IDs
    	for(var i=0;i<goodsList.length;i++){
    		if(goodsList.length == 1){
    			spuIds = goodsList[i].spuId;
    			mainImages = goodsList[i].img;
    		}else{
    			if(i != goodsList.length-1){
    				if(spuIds == null){
    	    			spuIds = goodsList[i].spuId+",";
    	    			mainImages = goodsList[i].img+",";
    	    		}else{
    	    			spuIds = spuIds + goodsList[i].spuId+",";
    	    			mainImages = mainImages + goodsList[i].img+",";
    	    		}
    			}else{
    				spuIds = spuIds + goodsList[i].spuId;
    				mainImages = mainImages + goodsList[i].img;
    			}
    		}
    		
    	}
    	
		$("#form").form({
			ajax : {
				url : CONST_URL.SAVE_HOT+"?id="+editData.shareId+"&reorder="+reorder+"&title="+title+"&img="+img+"&topImg="+topImg+"&spuIds="+spuIds+"&mainImages="+mainImages
			},
			onSubmitSuccess : function(data) {
				$.stip({
					type : 'ok',//显示类型 有info、 ok、 danger
					text : '保存成功！',
					onBeforeHide : function() {
   	                	backMain();
					}
				});
			}
		}).form("submit");
	}
  
	// 重新加载表格
	function reloadTable() {
		wlcReorder = $('#wlCommission').combobox('getValue');	
		wlcsReorder = $('#wlCommissionShare').combobox('getValue');
		timeReorder = $('#createTime').combobox('getValue');
		createTable(wlcReorder,wlcsReorder,timeReorder);
		$('#dataList').datagrid('reload', {
			ajax : {
				param : {
					"goodsName": trimSpace($('#goodsName').val()),
					"spuPlatform":$('#spuPlatform').combobox('getValue'),
					"isJingping": trimSpace($('#isJingping').val()),
					"categoryId":$('#categoryId').combobox('getValue'),
					"isHot":$('#isHot').combobox('getValue')
				}
			}
		});
	}
	
	//创建表格
	function createTable(wlcReorder,wlcsReorder,timeReorder) {
		
		$('#dataList')
				.datagrid(
						{
							ajax : {
								url : CONST_URL.GOODS_LIST+"?wlcReorder="+wlcReorder+"&wlcsReorder="+wlcsReorder+"&timeReorder="+timeReorder,
								param : {	
									"goodsName": trimSpace($('#goodsName').val()),
									"spuPlatform":$('#spuPlatform').combobox('getValue'),
									"isJingping": trimSpace($('#isJingping').val()),
									"categoryId":$('#categoryId').combobox('getValue'),
									"isHot":$('#isHot').combobox('getValue'),
									"shareId": trimSpace($('#shareId').val())
								},
								success : function(data) { //成功 
									alert("Data Saved: " + msg);
								}
							},
							columns : [
									{
										field : 'id',
										hidden : true
									},
									{
										render : function(rowData){
											return '<input type="checkbox" id="id" name="id" value="'+rowData.id+'">';
										}
									},
									{
										field : 'reorder'
									},
									{
										field : 'goodsName'
									},
									{
										render : function(rowData){
											return '<img width="100" height="100" src="'+rowData.mainImageUrl+'">';
										}
									},
									{
										field : 'spuPlatform'
									},
									{
										field : 'categoryId'
									},
									{
										render : function(rowData){
											return '<p>原价：'+rowData.wlPrice+'</p><br/>'
												  +'<p>券后价：'+rowData.conPrice+'</p><br/>'
												  +'<p>佣金：'+rowData.wlCommission+'</p>';
										}
									},
									{
										field : 'createTime'
									},
									{
										render : function(rowData) {
											return $.funcbar({
														data : permission, //请求的数据
														type : 'text', //类型
														display : 7, //显示数量
														permit : [
																'${name_english}:setHot'
																 ]
													//需要的权限按钮
													});
										}
									}

							]
						});
	}
	
	//返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_HOT;
    }
	//跳转代发货页面
	function toHot() {
		window.location.href = CONST_URL.INIT_HOT;
	}
	function toShare() {
		window.location.href = CONST_URL.INIT_SHARE;
	}
	function toGoodsList(){
		window.location.href = CONST_URL.INIT_GOODS_LIST;
	}
	//关闭子页面
	function closeWindow(object, isReload) 
    {
        object.close();
        isReload===true  && window.location.reload();
    }
</script>
</body>
</html>